from Bases import setID
from osc_base import OSCBaseObject

class ParameterBase(OSCBaseObject):
    _saved_class_name = 'ParameterBase'
    #_saved_attributes = ['name', 'id', 'parent_attribute_id', 'val_min', 'val_max']
    _saved_attributes = ['parent_attribute_id']
    def __init__(self, **kwargs):
        self.parent_attribute = kwargs.get('parent_attribute')
        super(ParameterBase, self).__init__(**kwargs)
        if 'deserialize' not in kwargs:
            self.parent_attribute_id = getattr(self.parent_attribute, 'id', None)
    

class GlobalParameter(ParameterBase):
    _saved_class_name = 'GlobalParameter'
    _saved_attributes = ['name', 'id']
    def __init__(self, **kwargs):
        super(GlobalParameter, self).__init__(**kwargs)
        if 'deserialize' not in kwargs:
            self.name = kwargs.get('name', '')
            self.id = setID(kwargs.get('id', None))
            
class DefinitionParameter(ParameterBase):
    _saved_class_name = 'DefinitionParameter'
    _saved_attributes = ['value_min', 'value_max', 'GlobalParameter_id']
    def __init__(self, **kwargs):
        if not hasattr(self, 'GlobalParameter'):
            self.GlobalParameter = kwargs.get('GlobalParameter')
        super(DefinitionParameter, self).__init__(**kwargs)
        if 'deserialize' not in kwargs:
            self.GlobalParameter_id = self.GlobalParameter.id
            if self.saved_class_name == 'DefinitionParameter':
                self.value_min = kwargs.get('value_min', 0)
                self.value_max = kwargs.get('value_max', 0)
        
    @property
    def name(self):
        return self.GlobalParameter.name
    @property
    def id(self):
        return self.GlobalParameter.id
    @property
    def range_str(self):
        return str(self.value_min) + ' - ' + str(self.value_max)
        
class PatchedParameter(DefinitionParameter):
    _saved_class_name = 'PatchedParameter'
    _saved_attributes = ['DefinitionParameter_id']
    def __init__(self, **kwargs):
        self.DefinitionParameter = kwargs.get('DefinitionParameter')
        self.GlobalParameter = self.DefinitionParameter.GlobalParameter
        super(PatchedParameter, self).__init__(**kwargs)
        if 'deserialize' not in kwargs:
            self.DefinitionParameter_id = self.DefinitionParameter.id
    @property
    def value_min(self):
        return self.DefinitionParameter.value_min
    @property
    def value_max(self):
        return self.DefinitionParameter.value_max
    
    def is_active(self, value):
        if value >= self.value_min and value <= self.value_max:
            return True
